package p.ithorns.sample.websocket.core;

import p.ithorns.sample.websocket.model.WsUpMsg;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * WsMsgHandlerContext
 *
 * @author HtL
 * @date 2024/10/9 15:11
 * @since 1.0.0
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class WsMsgHandlerDelegate  {


    private final static Map<String, WsMsgHandler<?>> handlers = new ConcurrentHashMap<>();


    public static void register(String type, WsMsgHandler handler) {
        handlers.putIfAbsent(type, handler);
    }

    public static WsMsgHandler<?> getHandler(String key) {
        return handlers.get(key);
    }

    public static void handle(WsUpMsg msg) {
        String type = msg.getType();
        WsMsgHandler<?> handler = getHandler(type);
        handler.handle(msg);
    }

}